const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 检查正数
const checkPositiveNumber = num => {
  num = num.toString();
  return (/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(num));
}

// 自动计算分配金额
// arr的单元素结构 {key:'', value:''}
const autoPayCountMath = (arr, total) =>{
  for (var i = 0; i < arr.length; i++) {
    if (i == arr.length - 1) {
      arr[i].value = total;
    } else if (total * 1 <= arr[i].value * 1) {
      arr[i].value = total;
      total = 0;
    } else {
      total -= arr[i].value;
    }
  }
  return arr;
}

module.exports = {
  formatTime: formatTime,
  formatNumber,
  checkPositiveNumber,
  autoPayCountMath
}
